package com.gary.springcloud.call.service;

import com.gary.springcloud.call.entity.ExecutorResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.concurrent.*;

/**
 * @author peng_rui
 * @date 2023/1/9
 */
@Slf4j
@Service
public class ThService {
    private ExecutorService executorService = new ThreadPoolExecutor(5, 10, 5, TimeUnit.MINUTES, new LinkedBlockingQueue<>());

    public ExecutorResult getExecutorResult(FutureTask<ExecutorResult> futureTask) {
        executorService.execute(futureTask);

        ExecutorResult result = null;
        try {
            result = futureTask.get();
        } catch (ExecutionException e) {
            result = failResult();
        } catch (InterruptedException e) {
            result = failResult();
            result.setMsg("中断异常");
        }
        return result;
    }


    private ExecutorResult failResult() {
        ExecutorResult result = new ExecutorResult();
        result.setMsg("执行失败");
        return result;
    }
}
